package cn.yun.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
/*
演示Request对象 获取请求参数的功能
* getParameter
* getParameterValues
* getParameterNames
* getParameterMap
*/
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //post获取请求参数
        //根据参数名称获取参数值
        String username = request.getParameter("username");
        System.out.println(username);
        System.out.println("==============");

        //根据参数名称获取参数值的数组
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }//game   study


        //获取所有请求的参数名称
     /*   Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String name = parameterNames.nextElement();
            System.out.println(name);
            String value = request.getParameter(name);
            System.out.println(value);
            System.out.println("============");*/

        //获取所有请求的参数的map集合
            Map<String, String[]> parameterMap = request.getParameterMap();
            //遍历map集合
            Set<String> keySet = parameterMap.keySet();
            for (String name : keySet) {
                //获取键
                System.out.println(name);
                //获取键对应的值
                String[] values = request.getParameterValues(name);
                for (String value : values) {
                    System.out.println(value);
                }
                System.out.println("============");
            }

        }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // String username = request.getParameter("username");
        //  System.out.println(username);
        this.doPost(request,response);
    }
}
